﻿using System;
using System.Data;
using System.IO;
using FixEd;
using System.Windows.Forms;
using DelimitedFile;
using System.Diagnostics;
using FixEd.Parsers;

namespace Delimited
{
    public class DelimitedFile : IParser
    {
        String samplename = @"C:\HMS Sample File.txt";

        private string _Delimiter;
        private readonly DataSet _FileContent;
        private readonly Guid _ID;
        private bool IsHeaderOnFirstLine;


        public string FileName { get; set; }

        public int[] ColumnWidths { get; set; }

        public Guid ID
        {
            get
            {
                return _ID;
            }
        }

        public string Name
        {
            get
            {
                return "Delimited Files";
            }
        }

        public string Description
        {
            get
            {
                return "Handles Delimited Files";
            }
        }



        public DelimitedFile()
        {
            Guid.TryParse("{3991C980-AD8B-4DC4-A1F2-B211A345FE8F}", out _ID);
            FileName = samplename;
            _FileContent = new DataSet();
        }


        public DataSet ParseFile()
        {
            GetFileAndParams();
            Split();
            return _FileContent;
        }

        private void GetFileAndParams()
        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                CheckFileExists = true,
                Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                Title = "Choose a file to import",
            };

            if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName != string.Empty)
            {
                //FileName = ofd.FileName;
                DelimOptions d = new DelimOptions();
                if (d.ShowDialog() == DialogResult.OK)
                {
                    _Delimiter = d.Delimiters[0];
                    IsHeaderOnFirstLine = d.IsTopHeader;
                }
            }
        }

        public void Save(DataSet ds, string filePath)
        {
            throw new NotImplementedException();
        }



        private void Split()
        {
            StreamReader sr = new StreamReader(FileName);
            _FileContent.Tables.Add();
            string[] columns = sr.ReadLine().Split(_Delimiter.ToCharArray());
            ColumnWidths = new int[columns.Length];
            //if the first line does not contain the headings, we'll just use numbers
            if (IsHeaderOnFirstLine)
            {
                for (int i = 0; i < columns.Length; i++)
                {
                    string temp = columns[i].Replace("\n", "");
                    _FileContent.Tables[0].Columns.Add(temp);
                    ColumnWidths[i] = 0;
                }
            }
            else
            {
                for (int i = 0; i < columns.Length; i++)
                {
                    _FileContent.Tables[0].Columns.Add(i.ToString());
                    ColumnWidths[i] = columns[i].Length;
                }
                _FileContent.Tables[0].Rows.Add(columns);
            }

            string data = sr.ReadToEnd();
            string[] rows = data.Split("\n".ToCharArray());
            ProcessRows(rows);
        }

        private void ProcessRows(string[] rows)
        {
            foreach (string r in rows)
            {
                string[] items = r.Split(_Delimiter.ToCharArray());
                for (int x = 0; x < items.Length; x++)
                {
                    if (items[x].Length > ColumnWidths[x])
                    {
                        ColumnWidths[x] = items[x].Length;
                    }
                }
                //split the row at the delimiter
                _FileContent.Tables[0].Rows.Add(items);
            }
        }

    }
}
